package com.acme.bankapp.service.email;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.email.Email;
import com.acme.bankapp.domain.email.impl.EmailImpl;
import com.acme.bankapp.service.bank.BankService;
import com.acme.bankapp.service.bank.EmailNotificationListener;

public class EmailServiceTester {
	static final String MES_CANNOT_LOAD_BANK = "Cannot load bank from file";

	public static void main(final String[] args) {
		new EmailServiceTester().go();
	}

	private void go() {
		EmailService service = EmailService.getEmailService();
		Bank bank;
		try {
			bank = BankService.readBank();
		} catch (ClassNotFoundException | IOException e) {
			System.out.println(MES_CANNOT_LOAD_BANK);
			return;
		}
		
		bank.addListener(new EmailNotificationListener());

		for (Client c : bank.getClients()) {
			service.sendNotificationEmail(c);
			try {
				// just to test slow message transition
				Thread.sleep(500);
			} catch (InterruptedException e) {	}
		}

		service.close();
		System.out.println("About to close email service");

		for (Client c : bank.getClients()) {
			service.sendNotificationEmail(c);
		}
	}
}
